Type Casting and Control Structures in C

TYPE CASTING:


Converting from one data type to another is type casting.


Types

Implicit conversion: it is automatically converted by the compiler itself

Explicit conversion: It should be converted by the user/programmer.

 #include<stdio.h>


int main()
{
    int a = 10.5; //implicit conversion
   
    char b = (char) 90; //explict conversion datatypr var = (desired datatype) value;
   
    printf("\n%d", a);
//  printf("\n%0.2f", b);
   
    printf("\n%c", b);
    return 0;
   
}
/*Typecasting - used to covert the values
implicit conversion - it will be converted automatically by the compiler
explict conversion - user conversion
*/


CONTROL STRUCTURES

 Control Structures are just a way to specify the flow of control in programs.


 There are three basic types of flow of control, known as

Sequence logic, or sequential flow

Selection logic, or conditional flow

Iteration logic, or repetitive flow


Sequence Logic


Sequential logic as the name suggests follows a serial or sequential flow in which the flow depends on the series of instructions given to the computer. 


2. Selection Logic (Branching)


Selection Logic simply involves a number of conditions which decide one out of several written statements.


3. Iteration Logic (Looping)


It helps to repeat a statement a certain number of times until the condition is satisfied. 


Selection Logic (Branching) 

Selection Logic simply involves a number of conditions which decide one out of several written statements.


  • If statement

  • If…else statement

  • If… else if statement

  • Switch Statement



if statement


 Use the if statement to specify a block of C code to be executed if a condition is true.

 Syntax

 if (condition) 

{
  // block the code to be executed if the condition is true
  }


#include<stdio.h>
int main(){
    int a = 20;
    int b = 20;
   
    if(a<b) //condition -> true - execute
    //false - stop
    {
        printf("A is lesser than b ");
    }
}


if… else statement


 Use the if statement to specify a block of C code to be executed if a condition is true.

 Use the other statement to specify a block of code to be executed if the condition is false.

Syntax

if (condition)

 {
  // block the code to be executed if the condition is true

else 

{
  // block of code to be executed if the condition is false
}


#include<stdio.h>

int main()
{
    int age;
    printf("Enter the age : ");
    scanf("%d", &age);
   
    if(age>=18) //condition -> ture ->o/p exe
        {  
        printf("You are eligible to vote");
        }
    //Condition - false - message - exe
    else
    {
        printf("You are not eligible to vote"); }  
}


If… else if statement


 Use the else if statement to specify a new condition if the first condition is false.


Syntax


if (condition1)

 {
  // block of code to be executed if condition1 is true


else if (condition2) 

{
  // block of code to be executed if the condition1 is false and condition2 is true

else 

{
  // block of code to be executed if the condition1 is false and condition2 is false
}


#include<stdio.h>

int main()
{
   
    //user - input
    int mark;
    printf("Enter the mark : ");
    scanf("%d",&mark);
   
    //Condition - starts
   
    if (mark <= 100 && mark >= 90)
        printf("A+ Grade");
       
    else if (mark < 90 && mark >= 80) //<90 = 89 it will stop
        printf("A Grade");
       
    else if (mark < 80 && mark >= 70)
        printf("B Grade");
       
    else if (mark < 70 && mark >= 60)
        printf("C Grade");
       
    else if (mark < 60 && mark > 50)
        printf("D Grade");
       
    else
        printf(" Failed");
    return 0;
}


Switch Case Statement


Use the switch statement to select one of the many code blocks to be executed.


Syntax:

switch(expression)

 {
  case x:
    // code block
    break;
  case y:
    // code block



    break;
  default:
    // code block

}



This is how it works:

The switch expression is evaluated once

The value of the expression is compared with the values of each case

If there is a match, the associated block of code is executed


#include<stdio.h>

int main()
{  
    printf("\n1. Addition");
    printf("\n2. Subraction");
    printf("\n3. Multiplication");
    printf("\n4. Division");
    printf("\n5. Modulus" );
   
    printf("\nEnter your choice : ");
    int choice;
    scanf("%d", &choice);
   
   
   
    switch(choice)
    {
        case 1:    
            printf("Enter two values: ");
            int a,b;
            scanf("%d%d", &a, &b);
            printf("The sum of two number is : %d", a+b);
            break;
           
        case 2:
            printf("Enter two values:");
            int c,d;
            scanf("%d%d", &c, &d);
            printf("The subraction of two number is : %d", c-d);
            break;
             
        case 3:
            printf("Enter two values:");
            int f,g;
            scanf("%d%d", &f, &g);
            printf("The product of two number is : %d",f*g);
            break;
           
        case 4:
            printf("Enter two values:");
            int i,j;
            scanf("%d%d", &i, &j);
            printf("The division of two number is : %d",i/j);
            break;
           
        case 5:
           
            printf("Enter two values: ");
            int l,m;
            scanf("%d%d", &l, &m);
            printf("The remainder of two number is : %d",l%m);
            break;
           
        default:
            printf("Invalid choice");
//          break;
    }
    return 0;
}

Post a Comment

0 Comments